package com.us.jack.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.us.jack.dao.IUserDAO;
import com.us.jack.pojo.PageModel;
import com.us.jack.pojo.User;

public class UserDAO extends SqlMapClientDaoSupport implements IUserDAO {

	@SuppressWarnings("unchecked")
	public List<User> getUsersByKey(User user,PageModel<User> pageModel) throws RuntimeException {
		// TODO Auto-generated method stub
		 Map parameterObject= new HashMap();
		 if(null != user) {
			 if(null != user.getId()) {
				 parameterObject.put("id", user.getId());
			 }
			 parameterObject.put("userName", user.getUserName());
			 parameterObject.put("age", user.getAge());
			 parameterObject.put("birthday", user.getBirthday()); 
		 }
		 if(null != pageModel) {
			 parameterObject.put("firstResults", pageModel.getFirstResults());
			 parameterObject.put("pageSize", pageModel.getPageSize());
		 }		 
		return this.getSqlMapClientTemplate().queryForList("getUsersByKey",
				parameterObject);
	}

	public void insert(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().insert("insertUser", user);
	}

	public int countUsers() throws RuntimeException {
		// TODO Auto-generated method stub
		return (Integer)this.getSqlMapClientTemplate().queryForObject("countUsers");
	}

	public void update(User user) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("updateUser", user);
	}

	public void delete(User user) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("deleteUser", user);
	}

}
